﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LayerBUS;
using LayerDTO;

namespace PluginQuanTri
{
    public partial class FormQuanLyPhieuNhap : Form
    {
        public FormQuanLyPhieuNhap()
        {
            InitializeComponent();
        }
        public List<CT_PhieuNhapDTO> lisctPN = new List<CT_PhieuNhapDTO>();
        XuLyChuoiKetNoiDTO xlChuoi = new XuLyChuoiKetNoiDTO();

        //void LoadChiTietPhieuNhap()
        //{
        //    xlChuoi = XuLyChuoiKetNoiBUS.DocChuoiKetNoi("connection.xml");

        //    gridChiTietPhieuNhap.DataSource = CT_PhieuNhapBUS.LayDanhSachCTPhieuNhap(xlChuoi.ChuoiKetNoi,);
        //    gridChiTietPhieuNhap.Columns.Remove("DaXoa");
        //    LoadChiTietPhieuNhap();

        //}
        private void FormQuanLyPhieuNhap_Load(object sender, EventArgs e)
        {
            xlChuoi = XuLyChuoiKetNoiBUS.DocChuoiKetNoi("connection.xml");

            gridPhieuNhap.DataSource = PhieuNhapBUS.LayDanhSachPhieuNhap(xlChuoi.ChuoiKetNoi);
            gridPhieuNhap.Columns.Remove("DaXoa");

           
           // gridChiTietPhieuNhap.DataSource = CT_PhieuNhapBUS.LayDanhSachCTPhieuNhap(xlChuoi.ChuoiKetNoi, "1");

        }

        private void btnXoaPN_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridPhieuNhap.SelectedRows.Count > 0)
                {
                    string _maPN = gridPhieuNhap.SelectedRows[0].Cells[0].Value.ToString();
                    xlChuoi = XuLyChuoiKetNoiBUS.DocChuoiKetNoi("connection.xml");
                    PhieuNhapDTO pnDto=new PhieuNhapDTO();
                    pnDto.MaPN=_maPN;
                    pnDto.NgayNhap=DateTime.Parse(gridPhieuNhap.SelectedRows[0].Cells[1].Value.ToString());
                    pnDto.DaXoa=true;

                    PhieuNhapBUS.SuaPhieuNhap(pnDto,xlChuoi.ChuoiKetNoi);
                    lisctPN = CT_PhieuNhapBUS.LayDanhSachCTPhieuNhap(xlChuoi.ChuoiKetNoi, _maPN);
                    for (int i = 0; i < lisctPN.Count; i++)
                    {
                        lisctPN[i].DaXoa = true;
                        CT_PhieuNhapBUS.SuaCTPhieuNhap(lisctPN[i], xlChuoi.ChuoiKetNoi);
                    }

                    gridPhieuNhap.DataSource = PhieuNhapBUS.LayDanhSachPhieuNhap(xlChuoi.ChuoiKetNoi);
                    gridPhieuNhap.Columns.Remove("DaXoa");

                    gridChiTietPhieuNhap.DataSource = CT_PhieuNhapBUS.LayDanhSachCTPhieuNhap(xlChuoi.ChuoiKetNoi, _maPN);

                    gridChiTietPhieuNhap.Columns.Remove("DaXoa");
                    gridChiTietPhieuNhap.Columns.Remove("MaCTPN");
                    gridChiTietPhieuNhap.Columns.Remove("MaPN");
                    gridChiTietPhieuNhap.Columns.Remove("MaMatHang");
                    gridChiTietPhieuNhap.Columns.Remove("MaDV");
                     MessageBox.Show(" Xóa thông tin tác giả thành công. ", "QLVLXD");
                }
            }
            catch { }

        }

        private void btnThoatQLPN_Click(object sender, EventArgs e)
        {
            Close();
        }
        
        private void gridPhieuNhap_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (gridPhieuNhap.SelectedRows.Count > 0)
                {
                    cbxMatHang.DataSource = MatHangBUS.LayDanhSachMatHang(xlChuoi.ChuoiKetNoi);
                    cbxMatHang.DisplayMember = "TenMatHang";
                    cbxMatHang.ValueMember = "MaMatHang";

                    cbxDonVi.DataSource = DonViBUS.LayDanhSachDonVi(xlChuoi.ChuoiKetNoi);
                    cbxDonVi.DisplayMember = "TenDV";
                    cbxDonVi.ValueMember = "MaDV";
                    string _maPN = gridPhieuNhap.SelectedRows[0].Cells[0].Value.ToString();
                    xlChuoi = XuLyChuoiKetNoiBUS.DocChuoiKetNoi("connection.xml");

                    gridChiTietPhieuNhap.DataSource = CT_PhieuNhapBUS.LayDanhSachCTPhieuNhap(xlChuoi.ChuoiKetNoi, _maPN);
                    //cbxMatHang.ReadOnly.ToString();
                    //cbxDonVi.ReadOnly.ToString();

                    gridChiTietPhieuNhap.Columns.Remove("DaXoa");
                    gridChiTietPhieuNhap.Columns.Remove("MaCTPN");
                    gridChiTietPhieuNhap.Columns.Remove("MaPN");
                    gridChiTietPhieuNhap.Columns.Remove("MaMatHang"); 
                    gridChiTietPhieuNhap.Columns.Remove("MaDV");

                }
            }
            catch { }
        }

     

      
    }
}
